import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store'

Vue.use(VueRouter) // 路由规则 const routes = [ { path: '/login', name: 'login', component: Login },{ path: '/', component: Layout, children: [ { path: '', name: 'home', component: Home },{ path: '/role', name: 'role', component: Role },{ path: '/menu', name: 'menu', component: Menu },{ path: '/resource', name: 'resource', component: Resource },{ path: '/course', name: 'course', component: Course },{ path: '/user', name: 'user', component: User },{ path: '/advert',

const routes = [
  { path: '/login', name: 'login', component: () => import(/* webpackChunkName: 'login' */'@/views/login/index') },
  {
    path: '/',
    component: () => import(/* webpackChunkName: '/' */ '@/views/layout/index'),
    meta: { requireAuth: true },
    redirect: '/login',
    children: [
      { path: '', name: 'home', component: () => import(/* webpackChunkName: 'home' */'@/views/home/index') },
      { path: '/role', name: 'role', component: () => import(/* webpackChunkName: 'role' */'@/views/role/index') },
      { path: '/menu', name: 'menu', component: () => import(/* webpackChunkName: 'menu' */'@/views/menu/index') },
      { path: '/resource', name: 'resource', component: () => import(/* webpackChunkName: 'resource' */'@/views/resource/index') },
      { path: '/course', name: 'course', component: () => import(/* webpackChunkName: 'course' */'@/views/course/index') },
      { path: '/user', name: 'user', component: () => import(/* webpackChunkName: 'user' */'@/views/user/index') },
      { path: '/advert', name: 'advert', component: () => import(/* webpackChunkName: 'advert' */'@/views/advert/index') },
      { path: '/advert-space', name: 'advert-space', component: () => import(/* webpackChunkName: 'advert-space' */'@/views/advert-space/index') },
      { path: '/menu/create', name: 'menu-create', component: () => import(/* webpackChunkName: 'menu-create' */'@/views/menu/components/create') },
      { path: '/menu/:id/update', name: 'menu-update', component: () => import(/* webpackChunkName: 'menu-update' */'@/views/menu/components/update') },
      { path: '/role/:roleId/alloc-menu', name: 'alloc-menu', component: () => import(/* webpackChunkName: 'alloc-menu' */'@/views/role/components/alloc-menu'), props: true },
      { path: '/course/create', name: 'create', component: () => import(/* webpackChunkName: 'create' */'@/views/course/create') }
    ]
  },
  { path: '*', name: 'error-page', component: () => import(/* webpackChunkName: 'error-page' */'@/views/error-page/index') }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  if (to.matched.some(record => record.meta.requireAuth)) {
    // 验证 Vuex 的store 中的登录 用户信息是否存储
    if (!store.state.user) {
      // 未登录，跳转到登录页面
      return next({
        path: '/login',
        query: {
          // 将本次路由的 fullpath 传递给 login页面
          redirect: to.fullPath
        }
      })
    }
  }
  next()
})

export default router
